/*
* Conditions Of Use
*
* This software was developed by employees of the Sigmatrix(Beijing) Corporation.
* This software is provided by sigmatrix as a service and is expressly
* provided "AS IS."  Sigmatrix MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  Sigmatrix does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement.
*
*/
package com.sigmatrix.h5.web.controller;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sigmatrix.h5.entity.MultistageLevelQRCode;
import com.sigmatrix.h5.enums.ResponseEnum;
import com.sigmatrix.h5.response.Response;
import com.sigmatrix.h5.service.MultistageLevelQrCodeService;
import com.sigmatrix.h5.utils.Cons;
import com.sigmatrix.h5.web.controller.common.BaseController;
import com.sigmatrix.h5.web.vo.MultistageLevelQRCodeVo;
import com.sigmatrix.h5.web.vo.PackScanVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 *@ClassName: MultistageLevelQRCodeController
 *@Description: TODO()
 *@author youjun
 *@date 2017年8月22日 上午10:21:05
 */

@ApiIgnore
@Api(tags="多级码查询更新操作")
@RestController
@RequestMapping(value="/app/multi")
public class MultistageLevelQRCodeController extends BaseController {

	/**多级码业务层接口*/
	@Autowired
	private transient MultistageLevelQrCodeService multistageLevelQRCodeService;
	
	/**
	 *@Title: getMulti
	 *@Description: TODO()
	 *@return Response
	 *@author youjun
	 *@date 2017年8月22日 上午10:46:07
	 *@param enterpriseCode
	 *@param code
	 *@param codeType
	 *@return
	 */
	@ApiOperation(value="查询二维码", notes="查询二维码")
	@PostMapping(value="/getmulti")
	public Response getMulti(@RequestBody final PackScanVo packScanVo) {
		
		try{
			final boolean isCons = packScanVo.getCodeType().intValue()==Cons.CODE_TYPE_STANDARD?true:false;
			final MultistageLevelQRCode multis = 
					multistageLevelQRCodeService.findMultistageLevelQRCodeByQRCode(packScanVo.getEnterpriseCode(), 
					packScanVo.getCode(), 
					packScanVo.getCodeType(), isCons);
			if(multis==null) {
				return new Response(ResponseEnum.NO_DATA);
			}
			return new Response(ResponseEnum.SUCCESS, multis);
		}catch(Exception e){
			return new Response(ResponseEnum.EXCEPTION);
		}
	}
	
	@ApiOperation(value="更新一个二维码", notes="更新一个二维码")
	@PostMapping(value="/updateone")
	public Response updateOne(@RequestBody final MultistageLevelQRCodeVo multis) {
		
		try{
			final MultistageLevelQRCode multistageLevelQRCode = multistageLevelQRCodeService.findMultistageLevelQRCodeByQRCode(multis.getEnterpriseCode(), 
					multis.getPackContent(), Cons.CODE_TYPE_CIPHER, false);
			if(multistageLevelQRCode==null) {
				return new Response(ResponseEnum.NO_DATA);
			}
			final MultistageLevelQRCode updateMultis = new MultistageLevelQRCode();
			BeanUtils.copyProperties(multis, updateMultis);
			multistageLevelQRCodeService.updateOne(updateMultis);
			// 更新后再查一遍
			final MultistageLevelQRCode newMultis = multistageLevelQRCodeService.findMultistageLevelQRCodeByQRCode(multis.getEnterpriseCode(), 
					multis.getPackContent(), Cons.CODE_TYPE_CIPHER, false);
			return new Response(ResponseEnum.SUCCESS, newMultis);
		}catch(Exception e){
			return new Response(ResponseEnum.EXCEPTION);
		}
	}
}
